package cn.edu.wynu.service;

import cn.edu.wynu.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;

@Component
public class EmployeeService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    PlatformTransactionManager platformTransactionManager;

    @Transactional
    public void testTransaction(String s) {
        Employee employee1 = jdbcTemplate.queryForObject("select * from t_emp where emp_id = 1",
                new BeanPropertyRowMapper<>(Employee.class));
        Employee employee2 = jdbcTemplate.queryForObject("select * from t_emp where emp_id = 2",
                new BeanPropertyRowMapper<>(Employee.class));

        String sql1 = "update t_emp set emp_salary = ? where emp_id = 1";
        String sql2 = "update t_emp set emp_salary = ? where emp_id = 2";
        double salary1 = employee1.getEmpSalary() - 3;
        double salary2 = employee2.getEmpSalary() + 3;

        int update1 = jdbcTemplate.update(sql1, salary1);
        String s1 = s.toLowerCase();
        int update2 = jdbcTemplate.update(sql2, salary2);
    }
}
